package net.tooeye.easymes.activities;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import net.tooeye.easymes.common.BaseActivity;
import net.tooeye.easymes.common.NetworkUtil;
import net.tooeye.easyplus.R;

public class TransferActivity extends BaseActivity {

    private TextView textViewInfo;
    private EditText edit_transfer_bar;
    private Button buttonPost;
    private  AlertDialog sfctaDialog;
    private  TextView textViewFrom;
    private  TextView textViewTo;
    private  EditText editTextSelect;
    private  EditText editTextFrom,editTextTo;

    private EditText  selectEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer);
        hideActionBar();
        init();
    }

    private void init() {
        textViewInfo = findViewById(R.id.textViewInfo);
        edit_transfer_bar = findViewById(R.id.edit_transfer_bar);
        edit_transfer_bar.setOnEditorActionListener(new TextEnterListener());
        buttonPost = findViewById(R.id.buttonPost);
        buttonPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                @SuppressLint("HandlerLeak")Handler handler = new Handler(){
                    public void handleMessage(Message msg) {
                        String result = (String)msg.obj;
                        Log.d("返回", result);
                    }
                };
                String url = "http://192.168.2.116:63368/api/moctacard/getcard?bar=123456";
                NetworkUtil.getInstance().get(TransferActivity.this,url,null,handler,1,TransferActivity.this.GetToken());
            }
        });
        textViewFrom = findViewById(R.id.textViewFromSfcta);
        textViewTo = findViewById(R.id.textViewToSfcta);
        editTextFrom = findViewById(R.id.editTextFrom);
        editTextTo = findViewById(R.id.editTextTo);
        textViewFrom.setOnClickListener(new OpenSfctaFromSelect());
        textViewTo.setOnClickListener(new OpenSfctaToSelect());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try{
            if ( requestCode == 1){
                editTextFrom.setText(data.getStringExtra("sfcta"));
            }
            if(requestCode == 2){
                editTextTo.setText(data.getStringExtra("sfcta"));
            }
        }
        catch (Exception e){

        }

    }

    //回车处理事件
    class TextEnterListener implements TextView.OnEditorActionListener{
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            Log.e("一个回车", actionId + "\t");
            String bar = edit_transfer_bar.getText().toString();
            String url = GetHost() + "/api/moctacard/getcard/?bar=" + bar;
            @SuppressLint("HandlerLeak")Handler handler = new Handler(){
                public void handleMessage(Message msg) {

                }
            };
            NetworkUtil.getInstance().get(TransferActivity.this,url,null,handler,1,GetToken());
            return false;
        }
    }

    class  OpenSfctaFromSelect implements  View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(TransferActivity
                    .this,SfctaSelectActivity.class);
            startActivityForResult(intent,1);
        }
    }

    class  OpenSfctaToSelect implements  View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(TransferActivity
                    .this,SfctaSelectActivity.class);
            startActivityForResult(intent,2);
        }
    }
}
